﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace GMME.Util
{
    public class TransferUtil
    {
        public static AndroidJavaObject DictToMap(Dictionary<string, string> dictionary)
        {
            AndroidJavaObject map = new AndroidJavaObject("java.util.HashMap");

            if (dictionary == null)
            {
                return map;
            }

            foreach (var pair in dictionary)
            {
                map.Call<string>("put", pair.Key, pair.Value);
            }

            return map;
        }

        public static Dictionary<string, string> MapToDict(AndroidJavaObject androidJavaObject)
        {
            Dictionary<string, string> dictionary = new Dictionary<string, string>();

            if (androidJavaObject == null)
            {
                return dictionary;
            }

            AndroidJavaObject set = androidJavaObject.Call<AndroidJavaObject>("keySet");
            AndroidJavaObject javaList = new AndroidJavaObject("java.util.ArrayList", set);
            if (javaList != null)
            {
                int count = javaList.Call<int>("size");
                for (int i = 0; i < count; i++)
                {
                    string key = javaList.Call<string>("get", i);
                    string value = androidJavaObject.Call<string>("get", key);
                    dictionary.Add(key, value);
                }
            }

            return dictionary;
        }

        public static AndroidJavaObject CsharpListToJavaList<T>(List<T> csharpList)
        {
            AndroidJavaObject javaList = new AndroidJavaObject("java.util.ArrayList");

            if (csharpList == null || csharpList.Count == 0)
            {
                return javaList;
            }

            foreach (var element in csharpList)
            {
                javaList.Call<bool>("add", element);
            }

            return javaList;
        }

        public static List<AndroidJavaObject> JavaListToCsharpList(AndroidJavaObject javaList)
        {
            if (javaList == null)
            {
                return new List<AndroidJavaObject>();
            }

            int count = javaList.Call<int>("size");
            List<AndroidJavaObject> csharpList = new List<AndroidJavaObject>(count);
            for (int i = 0; i < count; i++)
            {
                csharpList.Add(javaList.Call<AndroidJavaObject>("get", i));
            }

            return csharpList;
        }

        public static void CsharpArrayToJavaArray(AndroidJavaObject javaObject, byte[] data, string methodName)
        {
            if (data == null || data.Length == 0)
            {
                return;
            }

            Debug.Log(data.ToString());
            IntPtr jAryPtr = AndroidJNIHelper.ConvertToJNIArray(data);
            jvalue[] blah = new jvalue[1];
            blah[0].l = jAryPtr;

            IntPtr methodId = AndroidJNIHelper.GetMethodID(javaObject.GetRawClass(), methodName);
            AndroidJNI.CallVoidMethod(javaObject.GetRawObject(), methodId, blah);
        }
    }
}